package com.zmm.util.auth.filter;

import com.zmm.util.auth.enums.UserType;
import com.zmm.util.auth.model.AuthParam;
import com.zmm.util.auth.registry.annotation.Authentications;
import org.springframework.web.method.HandlerMethod;

/**
 * @author 900045
 * @description:
 * @name UserTypeFilter
 * @date By 2021-07-06 09:56:04
 */
public class UserTypeFilter implements AuthFilter {

	@Override
	public boolean doFilter(HandlerMethod handler, AuthParam param) {
		//1.查看类上是否有加认证类
		Authentications annotation = handler.getBeanType().getAnnotation(Authentications.class);
		if (annotation == null) {
			//2.查看方法 是否有加认证类
			annotation = handler.getMethodAnnotation(Authentications.class);
		}
		if (annotation == null || annotation.userType() == null
				|| annotation.userType().length == 0) { //没有注解 或者没有限制类型  则返回成功 
			return true;
		}
		//此时 用户已经不能为空了
		if (param.getUser() == null) {
			return false;
		}
		for (UserType userType : annotation.userType()) {
			if (userType.code.equals(param.getUser().getUserType())) {
				return true;
			}
		}
		return false;
	}
}
